package com.nus;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import com.nus.parsers.ParserAPIs;
import com.nus.representations.IvleLibrary;
import com.nus.representations.IvleModule;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class IvleLibraryActivity extends Activity {
	private ListView lv;
	private ArrayList<IvleLibrary> reserve;
	protected TextView title;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.ivlelibrarylayout);
		 getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
		 title = (TextView) findViewById(R.id.titleheader);
		 title.setText("Library E-reserve");
		 ImageView b = (ImageView)findViewById(R.id.home);
		  b.setImageDrawable(getResources().getDrawable(R.drawable.ivle));
		String url = "https://ivle.nus.edu.sg/api/Lapi.svc/LibEreserves?APIKey=W7RtgW7GZKUpaEkJ1NwUD&AuthToken="
				+ NUS_Android.IVLE_Token
				+ "&CourseID="
				+ IvleModuleActivity.COURSE_ID + "&TitleOnly=false&output=json";
		try {
			String response = getResponseText(url);

			Log.i("Response", response);

			reserve = ParserAPIs.parseIvleELibrary(response);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		lv = (ListView) findViewById(R.id.list_view);
		lv.setAdapter(new ListAdapter(IvleLibraryActivity.this,
				R.id.module_item, reserve));

	}

	public String getResponseText(String stringUrl) throws IOException {
		StringBuilder response = new StringBuilder();

		URL url = new URL(stringUrl);
		HttpURLConnection httpconn = (HttpURLConnection) url.openConnection();
		if (httpconn.getResponseCode() == HttpURLConnection.HTTP_OK) {
			BufferedReader input = new BufferedReader(new InputStreamReader(
					httpconn.getInputStream()), 8192);
			String strLine = null;
			while ((strLine = input.readLine()) != null) {
				response.append(strLine);
			}
			input.close();
		}
		return response.toString();
	}

	private class ListAdapter extends ArrayAdapter<IvleLibrary> { // --
		// CloneChangeRequired
		private ArrayList<IvleLibrary> mList; // --CloneChangeRequired
		private Context mContext;

		public ListAdapter(Context context, int textViewResourceId,
				ArrayList<IvleLibrary> list) { // --CloneChangeRequired
			super(context, textViewResourceId, list);
			this.mList = list;
			this.mContext = context;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			View view = convertView;
			try {
				if (view == null) {
					LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
					view = vi.inflate(R.layout.ivlemoduleitem, null);
				}
				final IvleLibrary listItem = mList.get(position);
				if (listItem != null) {
					// setting list_item views
					TextView tv = (TextView) view
							.findViewById(R.id.module_name);
					tv.setText(listItem.getFileDescription());
					TextView tv2 = (TextView) view.findViewById(R.id.semester);
					tv2.setText(listItem.getFileName());
					view.setOnClickListener(new OnClickListener() {
						public void onClick(View arg0) { // --clickOnListItem
							if (listItem.getFileSize().equals("0")) {
								String url = listItem.getFileName();
								Intent i = new Intent(Intent.ACTION_VIEW);
								i.setData(Uri.parse(url));
								startActivity(i);
							} else {

							}

							finish();
						}
					});
				}
			} catch (Exception e) {
				Log.i(IvleLibraryActivity.ListAdapter.class.toString(),
						e.getMessage());
			}
			return view;
		}
	}

}
